import { getNegatives, getSecuFromView, hasJianChi } from '@/server/controllers/secuController';
import { arrayCompare } from '@/lib/utils';

const styles = ['国企', '国企改革', '摘帽', '一季报增长', '并购重组', '半年报增长', '年报增长', '三季报增长', 'AI', '新能源'];

const NegativePage = async ({ params }) => {
    const { secuCode } = await params;
    const targetSecu = await getSecuFromView(secuCode);
    if (!targetSecu) return null;
    const isInJianChi = await hasJianChi(secuCode);
    const negativeSecus = (await getNegatives()) || [];
    let filteredSecus = [];
    let sameHySecus = [];
    const targetElements = [...(targetSecu.expect || []), ...(targetSecu.expect_brief || [])];
    negativeSecus.forEach((item) => {
        const expect_brief = item.expect_brief || [];
        const result = arrayCompare(targetElements, expect_brief);
        if (result.length > 0 && item.secu_code !== targetSecu.secu_code) {
            item.crossed = result.filter((c) => !styles.includes(c));
            item.styles = result.filter((c) => styles.includes(c));
            filteredSecus.push(item);
        }
        if (item.hy === targetSecu.hy && item.secu_code !== targetSecu.secu_code) {
            sameHySecus.push(item);
        }
    });

    return (
        <div className="h-full w-full" style={{ fontSize: '0.5rem' }}>
            <p style={{ color: 'cyan' }}>
                {isInJianChi ? <span style={{ color: 'lime' }}>减持</span> : ''} {targetSecu.secu_name} （{targetSecu.hy}）
            </p>
            {(targetSecu.expect || []).length > 0 ? (
                <p style={{ color: 'red' }}>{targetSecu.expect.join('；')}</p>
            ) : (
                <p>&nbsp;</p>
            )}
            <hr />
            {sameHySecus.length > 0 ? (
                <p style={{ color: 'lime' }}>行业： {sameHySecus.map((s) => s.secu_name).join('；')}</p>
            ) : (
                <p>&nbsp;</p>
            )}
            {filteredSecus.map((item) => (
                <div key={item.secu_code}>
                    <p>
                        {item.prev_lbn} | {item.chg} | {item.secu_name}
                    </p>
                    <p>
                        <span style={{ color: 'gray' }}> {item.styles.join('；')}</span>
                        {item.styles?.length > 0 ? '；' : ''}
                        <span style={{ color: 'lime' }}> {item.crossed.join('；')}</span>
                    </p>
                </div>
            ))}
        </div>
    );
};

export default NegativePage;
